package com.qiange.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qiange.common.Result;
import com.qiange.pojo.Employee;
import com.qiange.service.EmployeeService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;


@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    /**
     * 登录判断
     *
     * @param request
     * @param employee 前端接收过来请求体中的账号与密码，封装为实体类
     * @return
     */
    @PostMapping("/login")
    public Result<Employee> login(HttpServletRequest request, @RequestBody Employee employee) {
        String password = employee.getPassword();
        //对前端获取到的密码进行md5加密
        password = DigestUtils.md5DigestAsHex(password.getBytes(StandardCharsets.UTF_8));

        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper();
        //根据页面提交的用户名到数据库中进行查询
        lqw.eq(Employee::getUsername, employee.getUsername());
        Employee emp = employeeService.getOne(lqw);

        //查询出来的数据为空，说明用户不存在
        if (emp == null) {
            return Result.error("账户或密码不存在");
        }

        if (!emp.getPassword().equals(password)) {
            return Result.error("密码错误");
        }

        //状态为0，说明该账号已经被禁用
        if (emp.getStatus() == 0) {
            return Result.error("该账号已被禁用");
        }

        HttpSession session = request.getSession();
        session.setAttribute("employeeid", emp.getId());

        return Result.success(emp);
    }


    @PostMapping("/logout")
    public Result<String> logOut(HttpServletRequest request) {

        //清楚session中的用户数据
        request.getSession().removeAttribute("employeeid");

        return Result.success("退出成功");
    }

    /**
     * 添加员工
     * @param request
     * @param employee
     * @return
     */
    @PostMapping
    public Result<String> add(HttpServletRequest request,@RequestBody Employee employee){
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));

//        employee.setCreateTime(LocalDateTime.now());
//        employee.setUpdateTime(LocalDateTime.now());
//
//        Long employeeid = (Long) request.getSession().getAttribute("employeeid");
//
//        employee.setCreateUser(employeeid);
//        employee.setUpdateUser(employeeid);

        System.out.println(employee.getId());
        employeeService.save(employee);
        Long id = employee.getId();
        return Result.success("添加成功");
    }


    @GetMapping("/page")
    public Result<Page> selectByPage(int page, int pageSize, String name){
        Page byPage = new Page(page,pageSize);

        LambdaQueryWrapper<Employee> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(StringUtils.isNotEmpty(name),Employee::getName,name);
        lambdaQueryWrapper.orderByDesc(Employee::getUpdateTime);

        employeeService.page(byPage,lambdaQueryWrapper);

        return Result.success(byPage);
    }

    @GetMapping("/{id}")
    public Result<Employee> selectById(@PathVariable long id){
        Employee employee = employeeService.getById(id);
        if (employee!=null){
            return Result.success(employee);
        }
        return Result.error("该员工不存在");
    }

    @PutMapping
    public Result<String> update(HttpServletRequest request,@RequestBody Employee employee){
//        //修改时间
//        employee.setUpdateTime(LocalDateTime.now());
//        //修改人
//        Long id = (Long) request.getSession().getAttribute("employeeid");
//        employee.setUpdateUser(id);
        employeeService.updateById(employee);

        return Result.success("修改成功");
    }
}
